/**
 * Created by dukes on 2015/9/23.
 */
class EffectMgr {
    //public effectFps:number = 16;

    private frameItem:FrameItem;
    public name:string;
    private currentEffects:FrameEffect[];
    private config:any;
    public constructor(){
        this.name = "effectmgr";

        this.frameItem = new FrameItem();
        this.frameItem.callFun = this.update;
        this.frameItem.callObj = this;
        this.currentEffects = [];
        this.config = {};
    }

    public update(delta:number){
        var effect:FrameEffect;
        for(var key in this.currentEffects){
            effect = this.currentEffects[key];
            effect.update(delta);
        }
    }
    private playing = {};
    public playEffect(name:string,playTimes:number=1,posIndex:number=0,completCallObj:any=null, completCall:Function=null, completParams:any = null,dispose:boolean=false){
        if( PerformanceMonitor.getInstance().inLowModel) return;
        if( this.playing[name]) return;
        if( !FrameMgr.getInstance().isUnderControl(this.name))
            FrameMgr.getInstance().addControll(this.frameItem);

        this.playing[name] = true;
        var effect:FrameEffect = EffectMgr.produce();
        effect.reset();
        effect.playTimes = playTimes;
        var config = this.config[name];
        effect.data = config;
        effect.rx = config.posX;
        effect.ry = config.posY;
        effect.completeCallFun = completCall;
        effect.completeCallObj = completCallObj;
        effect.completeCallParam = completParams;
        effect.autoDispose = dispose;
        effect.checkLoad();
        this.currentEffects.push(effect);
        DisplayController.instance().addtoEffect(effect);
    }

    public stopEffectByName(name){
        var len = this.currentEffects.length;
        var effect:FrameEffect;
        for(var i =0; i < len; i++){
            effect = this.currentEffects[i];
            if( effect.data.name == name){
                this.stopEffect(effect);
                break;
            }
        }
    }

    public stopEffect(effect:FrameEffect){
        var idx = this.currentEffects.indexOf(effect);
        if( effect.autoDispose){
            effect.texture.dispose();
        }
        if( idx != -1){
            this.currentEffects.splice(idx,1);
        }
        EffectMgr.reclaim(effect);
        this.playing[effect.data.name] = false;
        DisplayController.instance().removeFromEffect(effect);
        if( this.currentEffects.length == 0)
            FrameMgr.getInstance().delController(this.name);
    }

    public initConfig(data:any){
        var effects:any[] = data.effects;
        var len = effects.length;
        var data:any;
        var effect:EffectConfig;
        for( var idx = 0; idx < len; idx++ ){
            data = effects[idx];
            effect = new EffectConfig(data);
            this.config[effect.name] = effect;
        }
    }
    private static _cache:FrameEffect[] = [];
    public static produce():FrameEffect{
        if( EffectMgr._cache.length != 0)
            return EffectMgr._cache.pop();
        return new FrameEffect();
    }

    public static reclaim(effect:FrameEffect){
        var idx = EffectMgr._cache.indexOf(effect);
        if( idx == -1){
            effect.reset();
            EffectMgr._cache.push(effect);
        }
    }

    private static _instance:EffectMgr;
    public static getInstance():EffectMgr{
        if( EffectMgr._instance == null)
            EffectMgr._instance = new EffectMgr();
        return EffectMgr._instance;
    }
}